<?php
/**
 * Copyright (c) 2008, Till Klampaeckel
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 *  * Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, this
 *    list of conditions and the following disclaimer in the documentation and/or
 *    other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * PHP Version 5
 *
 * @category   Net
 * @package    Net_DNS2
 * @subpackage Net_DNS2_Header
 * @author     Eric Kilfoil <edk@ypass.net>
 * @author     Marco Kaiser <bate@php.net>
 * @author     Florian Anderiasch <fa@php.net>
 * @author     Till Klampaeckel <till@php.net>
 * @copyright  1997-2008 The respective owners.
 * @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
 * @version    Release: @package_version@
 * @link       http://net-dns2.googlecode.com/
 */

/**
 * Net_DNS2_Abstract
 */
require_once 'Net/DNS2/Abstract.php';

/**
 * Dependencies
 * 
 * @todo Lazy-load!
 */
require_once 'Net/DNS2/RR/A.php';
require_once 'Net/DNS2/RR/AAAA.php';
require_once 'Net/DNS2/RR/NS.php';
require_once 'Net/DNS2/RR/CNAME.php';
require_once 'Net/DNS2/RR/PTR.php';
require_once 'Net/DNS2/RR/SOA.php';
require_once 'Net/DNS2/RR/MX.php';
require_once 'Net/DNS2/RR/TSIG.php';
require_once 'Net/DNS2/RR/TXT.php';
require_once 'Net/DNS2/RR/HINFO.php';
require_once 'Net/DNS2/RR/SRV.php';
require_once 'Net/DNS2/RR/NAPTR.php';

/**
 * Resource Record object definition
 *
 * Builds or parses resource record sections of the DNS  packet including
 * the answer, authority, and additional  sections of the packet.
 *
 * @category   Net
 * @package    Net_DNS2
 * @subpackage Net_DNS2_RR
 * @author     Eric Kilfoil <edk@ypass.net>
 * @author     Marco Kaiser <bate@php.net>
 * @author     Florian Anderiasch <fa@php.net>
 * @author     Till Klampaeckel <till@php.net>
 * @copyright  1997-2008 The respective owners.
 * @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
 * @version    Release: @package_version@
 * @link       http://net-dns2.googlecode.com/
 */
class Net_DNS2_RR extends Net_DNS2_Abstract
{
    var $name;
    var $type;
    var $class;
    var $ttl;
    var $rdlength;
    var $rdata;

    /**
     * Use Net_DNS2_RR::factory() instead
     */
    private function __construct()
    {
    }

    /**
     * Returns an RR object, use this instead of constructor
     *
     * @param mixed $rr_rdata Options as string, array or data
     * 
     * @return object Net_DNS2_RR or Net_DNS2_RR_<type>
     *
     * @see Net_DNS2_RR::new_from_array()
     * @see Net_DNS2_RR::new_from_data()
     * @see Net_DNS2_RR::new_from_string()
     */
    static function factory($rrdata, $update_type = '')
    {
        if (is_string($rrdata)) {
            $rr = Net_DNS2_RR::new_from_string($rrdata, $update_type);
        } elseif (count($rrdata) == 7) {
            list($name, $rrtype, $rrclass, $ttl, $rdlength, $data, $offset) = $rrdata;
            $rr = Net_DNS2_RR::new_from_data($name, $rrtype, $rrclass, $ttl,
                $rdlength, $data, $offset);
        } else {
            $rr = Net_DNS2_RR::new_from_array($rrdata);
        }
        return $rr;
    }

    static function new_from_data($name, $rrtype, $rrclass, $ttl, $rdlength, $data, $offset)
    {
        $rr           = new Net_DNS2_RR('getRR');
        $rr->name     = $name;
        $rr->type     = $rrtype;
        $rr->class    = $rrclass;
        $rr->ttl      = $ttl;
        $rr->rdlength = $rdlength;
        $rr->rdata    = substr($data, $offset, $rdlength);
        
        if (class_exists('Net_DNS2_RR_' . $rrtype)) {
            $scn = 'Net_DNS2_RR_' . $rrtype;
            $rr  = new $scn($rr, $data, $offset);
        }
        return $rr;
    }

    static function new_from_string($rrstring, $update_type = '')
    {
        $rr    = new Net_DNS2_RR('getRR');
        $ttl   = 0;
        $parts = preg_split('/[\s]+/', $rrstring);
        
        while (count($parts) > 0) {
            $s = array_shift($parts);
            if (!isset($name)) {
                $name = ereg_replace('\.+$', '', $s);
            } else if (preg_match('/^\d+$/', $s)) {
                $ttl = $s;
            } else if (!isset($rrclass) && ! is_null(Net_DNS2::classesbyname(strtoupper($s)))) {
                $rrclass = strtoupper($s);
                $rdata = join(' ', $parts);
            } else if (! is_null(Net_DNS2::typesbyname(strtoupper($s)))) {
                $rrtype = strtoupper($s);
                $rdata = join(' ', $parts);
                break;
            } else {
                break;
            }
        }

        /**
         * Do we need to do this?
         */
        $rdata = trim(chop($rdata));

        if (! strlen($rrtype) && strlen($rrclass) && $rrclass == 'ANY') {
            $rrtype  = $rrclass;
            $rrclass = 'IN';
        } else if (! isset($rrclass)) {
            $rrclass = 'IN';
        }

        if (! strlen($rrtype)) {
            $rrtype = 'ANY';
        }

        if (strlen($update_type)) {
            $update_type = strtolower($update_type);
            if ($update_type == 'yxrrset') {
                $ttl = 0;
                if (! strlen($rdata)) {
                    $rrclass = 'ANY';
                }
            } else if ($update_type == 'nxrrset') {
                $ttl     = 0;
                $rrclass = 'NONE';
                $rdata   = '';
            } else if ($update_type == 'yxdomain') {
                $ttl     = 0;
                $rrclass = 'ANY';
                $rrtype  = 'ANY';
                $rdata   = '';
            } else if ($update_type == 'nxdomain') {
                $ttl     = 0;
                $rrclass = 'NONE';
                $rrtype  = 'ANY';
                $rdata   = '';
            } else if (preg_match('/^(rr_)?add$/', $update_type)) {
                $update_type = 'add';
                if (!$ttl) {
                    $ttl = 86400;
                }
            } else if (preg_match('/^(rr_)?del(ete)?$/', $update_type)) {
                $update_type = 'del';
                $ttl         = 0;
                $rrclass     = $rdata ? 'NONE' : 'ANY';
            }
        }

        if (strlen($rrtype)) {
            $rr->name     = $name;
            $rr->type     = $rrtype;
            $rr->class    = $rrclass;
            $rr->ttl      = $ttl;
            $rr->rdlength = 0;
            $rr->rdata    = '';

            if (class_exists('Net_DNS2_RR_' . $rrtype)) {
                $scn = 'Net_DNS2_RR_' . $rrtype;
                return new $scn($rr, $rdata);
            }
            return $rr;
        }
        return null;
    }

    static function new_from_array($rrarray)
    {
        $rr = new Net_DNS2_RR('getRR');
        foreach ($rrarray as $k => $v) {
            $rr->{strtolower($k)} = $v;
        }

        if (! strlen($rr->name)) {
            return null;
        }
        if (! strlen($rr->type)){
            return null;
        }
        if (! $rr->ttl) {
            $rr->ttl = 0;
        }
        if (! strlen($rr->class)) {
            $rr->class = 'IN';
        }
        if (strlen($rr->rdata)) {
            $rr->rdlength = strlen($rr->rdata);
        }
        if (class_exists('Net_DNS2_RR_' . $rr->rrtype)) {
            $scn = 'Net_DNS2_RR_' . $rr->rrtype;
            return new $scn($rr, $rr->rdata);
        }
        return $rr;
    }

    public function string()
    {
        return $this->name . ".\t" .
            (strlen($this->name) < 16 ? "\t" : '') .
            $this->ttl  . "\t"  .
            $this->class. "\t"  .
            $this->type . "\t"  .
            $this->rdatastr();
    }

    public function rdatastr()
    {
        if ($this->rdlength) {
            return '; rdlength = ' . $this->rdlength;
        }
        return '; no data';
    }

    public function rdata(&$packetORrdata, $offset = '')
    {
        if ($offset) {
            return $this->rr_rdata($packetORrdata, $offset);
        } else if (strlen($this->rdata)) {
            return $this->rdata;
        } else {
            return null;
        }
    }

    public function rr_rdata($packet, $offset)
    {
        return (strlen($this->rdata) ? $this->rdata : '');
    }

    public function data(&$packet, $offset)
    {
        $data  = $packet->dn_comp($this->name, $offset);
        $data .= pack('n', Net_DNS2::typesbyname(strtoupper($this->type)));
        $data .= pack('n', Net_DNS2::classesbyname(strtoupper($this->class)));
        $data .= pack('N', $this->ttl);

        $offset += strlen($data) + 2;  // The 2 extra bytes are for rdlength

        $rdata = $this->rdata($packet, $offset);
        $data .= pack('n', strlen($rdata));
        $data .= $rdata;

        return $data;
    }
}
?>